using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using Microsoft.Windows.UserControls.ViewModels;
using System.Windows.Data;

namespace Microsoft.Windows.UserControls
{
    [StyleTypedProperty(Property = "ItemContainerStyle", StyleTargetType = typeof(LoggerViewItem))]
    public class LoggerView : TreeView
    {
        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(String), typeof(LoggerView), new PropertyMetadata(null,OnTitlePropertyChanged));
        
        public static readonly DependencyPropertyKey TitleVisibilityPropertyKey = DependencyProperty.RegisterReadOnly("TitleVisibility", typeof(Visibility), typeof(LoggerView), new PropertyMetadata(Visibility.Collapsed, OnTitleVisibilityPropertyChanged ));
        
        public static readonly DependencyProperty TitleVisibilityProperty = TitleVisibilityPropertyKey.DependencyProperty;
        
        public String Title
        {
            get
            {
                return (String)base.GetValue(TitleProperty);
            }
            set
            {
                base.SetValue(TitleProperty, value);
            }
        }

        public Visibility TitleVisibility
        {
            get
            {
                return (Visibility)base.GetValue(TitleVisibilityProperty);
            }
            protected set
            {
                base.SetValue(TitleVisibilityPropertyKey, value);
            }
        }

        internal List<LoggerViewItem> StatusPendingUpdateCollection { get; private set; }

        static LoggerView()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(LoggerView), new FrameworkPropertyMetadata(typeof(LoggerView)));
        }

        public LoggerView() : base()
        {
            base.SetValue(UIElement.IsEnabledProperty, false);
            StatusPendingUpdateCollection = new List<LoggerViewItem>();
            Loaded += OnLoggerViewLoaded;
        }

        public override void OnApplyTemplate()
        {
            if( GetBindingExpression( ItemsSourceProperty ) != null )
                ItemTemplate = GetItemTemplate(true);
            base.OnApplyTemplate();
        }

        internal static HierarchicalDataTemplate GetItemTemplate(Boolean isMaster)
        {
            var dataTemplate = new HierarchicalDataTemplate();
            dataTemplate.DataType = typeof(ILoggerViewItemViewModel);
            dataTemplate.ItemsSource = new Binding() {Path = new PropertyPath("Children")} ;
            if (isMaster)
                dataTemplate.ItemTemplate = GetItemTemplate(false);
            var contentControlfactory = new FrameworkElementFactory(typeof(ContentControl));
            contentControlfactory.SetBinding(ContentControl.ContentProperty, new Binding());
            contentControlfactory.SetValue(ContentControl.ContentTemplateProperty, LoggerViewItem.GetHeaderDataTemplate(true));
            dataTemplate.VisualTree = contentControlfactory;
            return dataTemplate;
        }

        void OnLoggerViewLoaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;

            if( StatusPendingUpdateCollection.Count > 0)
            {
                foreach (var item in StatusPendingUpdateCollection)
                    item.OnStatusChanged(item.Status);
            }
        }
        
        static void OnTitlePropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var control = sender as LoggerView;
            if (control == null)
                return;
            var title = args.NewValue as String;
            control.TitleVisibility = String.IsNullOrEmpty(title) ? Visibility.Collapsed : Visibility.Visible;
        }
        
        static void OnTitleVisibilityPropertyChanged(DependencyObject sender, DependencyPropertyChangedEventArgs args)
        {
            var control = sender as LoggerView;
            if (control == null)
                return;
            
            if (args.NewValue == null || !(args.NewValue is Visibility))
            {
                control.TitleVisibility = Visibility.Collapsed;
                return;
            }

            var titleVisibility = (Visibility)args.NewValue;
            
            if (titleVisibility == Visibility.Collapsed && !String.IsNullOrEmpty(control.Title))
                control.Title = null;
            else if(titleVisibility == Visibility.Visible && String.IsNullOrEmpty(control.Title) )
                control.TitleVisibility = Visibility.Collapsed;
        }
        
        protected override DependencyObject GetContainerForItemOverride()
        {
            return new LoggerViewItem();
        }

        protected override Boolean IsEnabledCore
        {
            get
            {
                return false;
            }
        }
    }
}